﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAPDavy.Entities;

namespace MAPDavy.Repository
{
    public class TestGradeRepository:MasterRepository,ITestGradeRepository
    {
        public void AddTestGrade(TestGrade testGrade)
        {
            Context.TestGrades.Add(testGrade);
            Context.SaveChanges();
        }
        
        public void RemoveTestGrade(TestGrade testGrade)
        {
            var existe = (from tg in Context.TestGrades
                        where tg.TermId == testGrade.TermId &&
                              tg.GradeId == testGrade.GradeId &&
                              tg.TestId == testGrade.TestId
                        select tg).SingleOrDefault();

            if (existe != null)
            {
                Context.Entry(existe).State = System.Data.EntityState.Modified;
                Context.SaveChanges(); 
            }
        }


        public bool CanRemove(TestGrade testGrade)
        {
            var existe = (from tg in Context.TestGrades
                          .Include("Scores")
                          where tg.TermId == testGrade.TermId &&
                                tg.GradeId == testGrade.GradeId &&
                                tg.TestId == testGrade.TestId
                          select tg).SingleOrDefault();

            if (existe == null)
            {
                return false;
            }

            if (existe.Scores.Count >0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
